export interface UserInfo {
  roles: string[];
  businessLine?: string;
}

export type DataScope =
  | { type: 'all' } // 超级管理员
  | { type: 'businessLine'; businessLine: string }; // 管理员/督导


export function getDataScope(user: UserInfo | null): DataScope {
  if (!user) return { type: 'all' };
  const { roles, businessLine } = user;
  const isAdmin = roles?.includes('admin');
  const isSupervisor = roles?.includes('supervisor');

  if (isAdmin && !businessLine) {
    return { type: 'all' };
  }
  if ((isAdmin || isSupervisor) && businessLine) {
    return { type: 'businessLine', businessLine };
  }
  return { type: 'all' };
} 
// 新的优化版本，直接使用当前角色判断
export function getDataScopeFromAuth(
  currentRole: string, 
  businessLine?: string
): DataScope {
  if (!currentRole) return { type: 'all' };
  // 直接使用当前角色判断
  const isAdmin = currentRole === 'admin';
  const isSupervisor = currentRole === 'supervisor';
  
  // 如果是管理员且没有业务线限制，返回全部权限
  if (isAdmin && !businessLine) {
    return { type: 'all' };
  }
  
  // 如果是管理员或督导且有业务线，返回业务线权限
  if ((isAdmin || isSupervisor) && businessLine) {
    return { type: 'businessLine', businessLine };
  }
  
  // 默认返回全部权限
  return { type: 'all' };
}